package com.nobleoxtechnology.weatherSMS;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class GeneralSettings implements View.OnClickListener
{
	Context context;
	SharedPreferences preferences;
	Activity activity;
	public GeneralSettings(Context context, Activity activity, SharedPreferences preferences)
	{
		this.context = context;
		this.activity = activity;
		this.preferences = preferences;

		EditText editZip = (EditText) activity.findViewById(R.id.editZipCode);
		editZip.setText(preferences.getString("zip", "11110"));
	}
	
	@Override
	public void onClick(View v)
	{
		final EditText editZip = (EditText) activity.findViewById(R.id.editZipCode);
		
		String zipcode = editZip.getText().toString();

		Integer zipRange = null;
		try
		{
			zipRange = new Integer(zipcode);
		}
		catch (Exception e)
		{
			Toast.makeText(context, "Invalid number for zip code.", Toast.LENGTH_LONG).show();								
		}
		if (zipRange != null)
		{
			if (zipRange < 9999 || zipRange > 999999 || zipRange == 11111)
			{
				Toast.makeText(context, "Invalid zip code range.", Toast.LENGTH_LONG).show();
			}
			else
			{
		        SharedPreferences weatherPref = preferences;
		        Editor weatherEditor = weatherPref.edit();
		        weatherEditor.putString("zip", zipcode);
		        weatherEditor.commit();
		        
				Toast.makeText(context, "Settings saved successfully!", Toast.LENGTH_LONG).show();
			}
		}
		else
		{
			Toast.makeText(context, "Invalid zip code.", Toast.LENGTH_LONG).show();
		}
	}
}
